/**
 * hnjz.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.hnjz;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hnjz.common.security.CtxUtil;
import com.hnjz.sys.function.FunMapper;
import com.hnjz.sys.function.Function;

/**
 * 用户登录后主页的帮助类
 * 
 * @author wumi
 * @version $Id: IndexManager.java, v 0.1 Feb 6, 2013 9:23:40 AM wumi Exp $
 */
@Service("indexManager")
public class IndexManager {
    /**日志*/
    private static final Log log = LogFactory.getLog(IndexManager.class);

    @Autowired
    private FunMapper        funMapper;

    /**
     * 用户登录后获取用户所具有的菜单
     * 
     * @return 
     */
    public JSONArray i() {
        JSONArray arr = new JSONArray();
        try {
            String roleId = CtxUtil.getCurUser().getRole();
            List<Function> re = funMapper.findByRoleId(roleId);
            for (Function ele : re) {
                JSONObject obj = new JSONObject();
                obj.put("id", ele.getId());
                if (StringUtils.isNotBlank(ele.getParent())) {
                    obj.put("pid",ele.getParent());
                }
                obj.put("name", ele.getFuncName());
                obj.put("href",StringUtils.defaultIfBlank(ele.getLinkaddr(), ""));
                if (log.isDebugEnabled()) {
                    log.debug("obj:" + obj);
                }
                arr.put(obj);
            }
        } catch (JSONException e) {
            log.error("用户登录查询菜单错误：", e);
        }
        return arr;
    }
}
